<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title th:text="#{admin.roles.title}">语言管理</title>
</head>
<body>
<cms-layout layout="admin/layout/master">
    <div id="app">
        <div>
            <div>
                <el-button type="primary" @click="showDialog()">添加语言</el-button>
            </div>

                <el-table :data="languages">
                    <el-table-column prop="code" label="语言代码"></el-table-column>
                    <el-table-column prop="name" label="语言名称"></el-table-column>
                    <el-table-column prop="isDefault" label="默认语言">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.isDefault" type="success">是</el-tag>
                            <el-tag v-else type="info">否</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="enabled" label="状态">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.enabled" type="success">启用</el-tag>
                            <el-tag v-else type="info">禁用</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" @click="showDialog(scope.row)">编辑</el-button>
                            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
        </div>

        <!-- 编辑对话框 -->
        <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
            <el-form :model="form" label-width="100px">
                <el-form-item label="语言代码">
                    <el-input v-model="form.code" placeholder="如：zh-CN"></el-input>
                </el-form-item>
                <el-form-item label="语言名称">
                    <el-input v-model="form.name" placeholder="如：简体中文"></el-input>
                </el-form-item>
                <el-form-item label="默认语言">
                    <el-switch v-model="form.isDefault"></el-switch>
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch v-model="form.enabled"></el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="handleSave">确定</el-button>
            </div>
        </el-dialog>
    </div>
</cms-layout>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                languages: [],
                dialogVisible: false,
                dialogTitle: '添加语言',
                form: {
                    id: null,
                    code: '',
                    name: '',
                    isDefault: false,
                    enabled: true
                }
            }
        },
        created() {
            this.loadData()
        },
        methods: {
            loadData() {
                axios.get('/admin/language/list').then(response => {
                    if (response.data.code === 0) {
                        this.languages = response.data.data
                    }
                })
            },
            showDialog(row) {
                this.dialogVisible = true
                if (row) {
                    this.dialogTitle = '编辑语言'
                    this.form = {...row}
                } else {
                    this.dialogTitle = '添加语言'
                    this.form = {
                        id: null,
                        code: '',
                        name: '',
                        isDefault: false,
                        enabled: true
                    }
                }
            },
            handleSave() {
                axios.post('/admin/language/save', this.form).then(response => {
                    if (response.data.code === 0) {
                        this.$message.success('保存成功')
                        this.dialogVisible = false
                        this.loadData()
                    } else {
                        this.$message.error(response.data.msg)
                    }
                })
            },
            handleDelete(row) {
                this.$confirm('确认删除该语言？', '提示', {
                    type: 'warning'
                }).then(() => {
                    axios.post(`/admin/language/delete/${row.id}`).then(response => {
                        if (response.data.code === 0) {
                            this.$message.success('删除成功')
                            this.loadData()
                        } else {
                            this.$message.error(response.data.msg)
                        }
                    })
                })
            }
        }
    })
</script>

</body>
</html> 